package com.scau.meetingbook.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scau.meetingbook.pojo.Employees;
import com.scau.meetingbook.mapper.EmployeesMapper;
import com.scau.meetingbook.service.EmployeesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scau.meetingbook.vo.ResultVO;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.List;
import java.util.Date;

@Service
public class EmployeesServiceImpl extends ServiceImpl<EmployeesMapper, Employees> implements EmployeesService {

    @Autowired
    private EmployeesMapper employeesMapper;

    @Override
    public ResultVO insertEmployees(Employees employees) throws Exception {
        employees.setCreateTime(new Date());
        employeesMapper.insert(employees);
        return ResultVO.success(null);
    }

    @Override
    public ResultVO<Employees> getEmployeesById(Integer id) throws Exception {
        return ResultVO.success(null, employeesMapper.selectById(id));
    }

    @Override
    public ResultVO updateEmployeesById(Employees employees) throws Exception {
        employees.setUpdateBy(1);
        employees.setUpdateTime(new Date());
        employeesMapper.updateById(employees);
        return ResultVO.success("成功", employees);
    }

    @Override
    public ResultVO deleteEmployeesById(Integer id) throws Exception {
        employeesMapper.deleteById(id);
        return ResultVO.success(null);
    }

    @Override
    public ResultVO login(Employees employees) throws Exception {
        Employees employee = employeesMapper.selectOne(new QueryWrapper<Employees>()
                .eq("name", employees.getName()));
        if (employee == null) {
            return ResultVO.fail("用户不存在");
        }
        if (!employee.getLoginPassword().equals(employees.getLoginPassword())) {
            return ResultVO.fail("密码错误");
        }
        if (employee.getStatus().equals("frozen")) {
            return ResultVO.fail("用户已冻结");
        }
        return ResultVO.success("成功", employee);
    }

    @Override
    public ResultVO register(Employees employees) throws Exception {
        Employees employee = employeesMapper.selectOne(new QueryWrapper<Employees>()
                .eq("name", employees.getName()));
        if (employee != null) {
            return ResultVO.fail("用户已存在");
        }

        employees.setStatus("pending");
        employees.setCreateBy(1);
        employees.setCreateTime(new Date());

        employeesMapper.insert(employees);

        return ResultVO.success("成功", employees);
    }

    @Override
    public ResultVO changeEmployeeStatus(Integer id, String status) throws Exception {
        QueryWrapper<Employees> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("employee_id", id);
        Employees employee = employeesMapper.selectOne(queryWrapper);
        if (employee == null) {
            return ResultVO.fail("用户不存在");
        }
        employee.setUpdateTime(new Date());
        employee.setUpdateBy(1);
        employee.setStatus(status);
        employeesMapper.update(employee, queryWrapper);
        return ResultVO.success("成功",employee);
    }

    @Override
    public ResultVO<Page<Employees>> getAllEmployees(Integer pageNum, Integer pageSize){
        if(pageNum == null){
            pageNum = 1;
        }
        if(pageSize == null)
            pageSize = 10;
        Page<Employees> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Employees> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        employeesMapper.selectPage(page, queryWrapper);
        return ResultVO.success("成功", page);


    }

}